package com.chen.ssm.web;

import com.chen.ssm.entity.Flight;
import com.chen.ssm.entity.PageBean;
import com.chen.ssm.entity.Ticketorder;
import com.chen.ssm.entity.User;
import com.chen.ssm.service.TicketOrderService;
import com.chen.ssm.util.DateUtil;
import com.chen.ssm.util.JsonDateValueProcessor;
import com.chen.ssm.util.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * com.chen.ssm.web
 * 作者      : 金博超
 * 描述      :
 * 创建日期  : 2020/11/11
 * 修改日期  :
 */
@Controller
@RequestMapping("/ticketOrder")
public class TicketOrderController {

  @Resource
  private TicketOrderService ticketOrderService;



  private String format = "yyyy-MM-dd HH:mm:ss";
  private JsonDateValueProcessor jsonDateValueProcessor = new JsonDateValueProcessor(format);


  @RequestMapping("/adminTicketOrderPage")
  public String aircraftList() {
    return "admin/ticketOrderManage";
  }



  @RequestMapping("/myTicketOrderList")
  public ModelAndView myTicketOrderList(HttpServletRequest request) {
ModelAndView mav = new ModelAndView();
    String currentUser = request.getSession().getAttribute("currentUser").toString();
    List<Ticketorder> ticketOrderList = ticketOrderService.getTicketOrderByUserName(currentUser);
    JsonConfig jsonConfig = new JsonConfig();
    jsonConfig.registerJsonValueProcessor(Date.class, jsonDateValueProcessor);
    JSONArray array = JSONArray.fromObject(ticketOrderList, jsonConfig);
    mav.addObject("ticketOrderList",array);
    mav.setViewName("main_userCenter");
    request.setAttribute("mainPage","ticket/myTicketOrderList.jsp");
    return mav;
  }




    //插入机票
    @RequestMapping("/saveTicketOrder")
    @ResponseBody
    public Object save(Ticketorder ticketorder, HttpServletResponse response, HttpServletRequest request) {
        JSONObject result = new JSONObject();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ordertime = sdf.format(date);
        DateUtil dateUtil = new DateUtil();

        try {
            ticketorder.setOrdertime(dateUtil.formatString(ordertime, format));

            ticketorder.setOrderno("NO"+System.currentTimeMillis());
            ticketorder.setPrice(Integer.parseInt(request.getParameter("price")));
            ticketorder.setNum(Integer.parseInt(request.getParameter("num")));
            ticketorder.setSpacetype(request.getParameter("spacetype"));
            ticketorder.setUserid(Integer.parseInt(request.getParameter("userid")));
            ticketorder.setTotalprice(Integer.parseInt(request.getParameter("totalprice")));
            ticketorder.setFlightid(Integer.parseInt(request.getParameter("flightid")));

            ticketOrderService.insert(ticketorder);

            result.put("success", true);
            ResponseUtil.write(response, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }










}
